remove(list=ls())

#install.packages("pacman") If installing of pacman required
library(pacman)

p_load(here, tidyverse, haven, modelsummary, cregg, sjPlot,
       list, ggpubr, survey, xtable, interactions, viridisLite,
       ggdist, patchwork, estimatr, margins, doBy, modelr, plyr)

#remotes::install_github("leeper/cregg") If installing of cregg required

colors2 <- c("#56B4E9", "#C5407EFF")
colors <- viridis(option = "magma", 
                  begin = 0.1, 
                  end = .8, 
                  direction = -1, 
                  n = 9)

###LOADING AND CLEANING US DATA###
dfUS <- read_dta("data/study1_USdata.dta")
dfUS <-dfUS%>% 
  mutate(res_party2= as.factor(res_party2),
         IDstrength=as.factor(IDstrength),  
         strengthOG=as.factor(strengthOG),
         ideo3_self=as.factor(ideo3_self),
         ideo3_char=as.factor(ideo3_char),
         extreme=as.factor(extreme),
         strongdislike=as.factor(strongdislike),
         partynegaffectcat=as.factor(partynegaffectcat),
         IDasked=as.factor(IDasked),
         villian=as.factor(villian),
         villiannum=as.numeric(villian),
         house=as.factor(house),
         woman =as.factor(woman),
         british =as.factor(british),
         heroes=as.factor(heroes),
         noheroes=as.factor(noheroes),
         imdb_quartiles=as.factor(imdb_quartiles),
         engagement_quartiles=as.factor(engagement_quartiles),
         person= as.factor(person),
         LGBT= as.factor(LGBT),
         sex= as.factor(sex),
         degree= as.factor(degree))

dfUS$res_party2 <- factor(dfUS$res_party2,
                          levels = c(0,1),
                          labels = c("Republicans", "Democrats"))
dfUS$IDasked <- factor(dfUS$IDasked,
                       levels = c(0,1),
                       labels = c("Out-group named", "In-group named"))
dfUS$strengthOG <- factor(dfUS$strengthOG,
                          levels = c(1,2,3),
                          labels = c("1. Very strong", "2. Fairly strong", "3. Not very strong"))
dfUS$strengthdummy <- factor(dfUS$strengthdummy,
                             levels = c(0,1),
                             labels = c("Not at all strong", "Very strong"))
dfUS$IDstrength <- factor(dfUS$IDstrength,
                          levels = c(1,2,3,4),
                          labels = c("1. Very strong", "2. Fairly strong", "3. Not very strong", "4. Squeezed"))
dfUS$extreme <- factor(dfUS$extreme,
                       levels = c(0,1,2),
                       labels = c("Centrist", "Ideologically moderate", "Ideologically radical"))
dfUS$strongdislike <- factor(dfUS$strongdislike,
                             levels = c(0,1),
                             labels = c("Below mean negative out-group affect", "Above mean negative out-group affect"))
dfUS$partynegaffectcat <- factor(dfUS$partynegaffectcat,
                                 levels = c(0,1,2,3),
                                 labels = c("Republican with low Democrat hate", "Republican with strong Democrat hate", "Democrat with low Republican hate", "Democrat with strong Repeublican hate"))
dfUS$woman <- factor(dfUS$woman,
                     levels = c(0,1),
                     labels = c("Man", "Woman"))
dfUS$british <- factor(dfUS$british,
                       levels = c(0,1),
                       labels = c("Not British", "British"))
dfUS$villian <- factor(dfUS$villian,
                       levels = c(0,1),
                       labels = c("Hero", "Villain"))
dfUS$villiancat <- factor(dfUS$villiancat,
                          levels = c(0,1,2,3),
                          labels = c("Strong Hero", "Hero", "Villain", "Strong Villain"))
dfUS$house <- factor(dfUS$house,
                     levels = c(1,2,3,4,5,6),
                     labels = c("Disney", "Game of Thrones", "Harry Potter", "Lord of the Ring", "Marvel", "Star Wars"))
dfUS$imdb_quartiles <- factor(dfUS$imdb_quartiles,
                              levels = c(1,2,3,4),
                              labels = c("Rating Q1", "Rating Q2", "Rating Q3", "Rating Q4"))
dfUS$engagement_quartiles <- factor(dfUS$engagement_quartiles,
                                    levels = c(1,2,3,4),
                                    labels = c("Enagement Q1", "Enagement Q2", "Enagement Q3", "Enagement Q4"))

dfUS$heroes <- factor(dfUS$heroes,
                      levels = c(1,2,3,4,5,6,7,8,9,10,
                                 11,12,13,14,15,16,17,18,19,20,
                                 21,22,23,24,25,26,27,28,29,30,
                                 31,32,33,34,35,36,37,38,39,40,
                                 41,42,43),
                      labels = c("Aladdin", "Albus Dumbledore", "Aragorn", "Arthur Weasley", "Arya Stark", "Bilbo Baggins", "Black Widow", "Brienne of Tarth", "Captain America", "Captain Marvel", "Catelynn Stark", "Cinderella", "Dobby", "Eddard Stark", "Galadriel", "Gandalf the White", "Harry Potter", "Hercules", "Hermione Granger", "Ironman", "Kristoff", "Luke Skywalker", "Mighty Thor","Molly Weasley", "Okoye", "Princess Aurora", "Princess Fiona", "Princess Lea", "Princess Tiana", "Prof. Minerva McGonagall", "Quasimodo", "Rey Palpatine", "Rocket", "Ronald Weasley", "Simba",
                                 "Snow White","Spiderman", "Tarzan", "Thor", "Valkyrie", "Wanda Maximoff", "Yoda", "Éowyn"))

dfUS$noheroes <- factor(dfUS$noheroes,
                        levels = c(1,2,3,4,5,6,7,8,9,10,
                                   11,12,13,14,15,16,17,18,19,20,
                                   21,22,23,24,25,26,27,28,29,30,
                                   31,32,33,34,35,36,37,38,39,40,
                                   41,42,43),
                        labels = c("Agatha Harkness", "Azog", "Bellatrix Lestrange", "Captain Phasma", "Cersei Lannister", "Cruella de Vil", "Darth Maul", "Darth Vader", "Dolores Umbridge", "Dr Facilier", "Draco Malfoy", "Ebony Maw", "Fairy Godmother (Shrek)", "Gaston", "Gorr", "Gríma Wormtongue", "Hades", "Hela", "Joffrey Baratheon", "King Pin", "Kylo Ren", "Lady Tremaine", "Loki", "Lucius Malfoy", "Lurtz", "Maleficent", "Mother Gothel", "Narcissa Malfoy", "Pansy Parkinson", "Petunia Dursley", "Proxima Midnight", "Queen Grimhilde", "Queen of Hearts", "Red Skull (Johann Schmidt)", "Rita Skeeter", "Ronan the Accuser",
                                   "Roose Bolton", "Saruman the Wise", "Scar", "Thanos", "Tywin Lannister", "Ultron", "Ursula"))
labels <- c(villian = "Hero vs Villain", villiancat = "Hero vs. Villain", woman = "Sex",
            house = "Media Franchise", british = "British typecast",
            imdb_quartiles = "IMDB Review", engagement_quartiles= "IDMB engagment")

###LOADING AND CLEANING UK DATA###

dfUK <- read_dta("data/study1_UKdata.dta")

dfUK <-dfUK%>% 
  mutate(res_party2= as.factor(res_party2),
         IDstrength=as.factor(IDstrength),
         strengthOG=as.factor(strengthOG),
         ideo3_self=as.factor(ideo3_self),
         RILEself=as.factor(RILEself),
         ideo3_char=as.factor(ideo3_char),
         extreme=as.factor(extreme),
         IDasked=as.factor(IDasked),
         strongdislike=as.factor(strongdislike),
         partynegaffectcat=as.factor(partynegaffectcat),
         villian=as.factor(villian),
         villiannum=as.numeric(villian),
         house=as.factor(house),
         woman =as.factor(woman),
         british =as.factor(british),
         heroes=as.factor(heroes),
         noheroes=as.factor(noheroes),
         imdb_quartiles=as.factor(imdb_quartiles),
         engagement_quartiles=as.factor(engagement_quartiles),
         partyIDcat=as.factor(partyIDcat),
         person= as.factor(person),
         LGBT= as.factor(LGBT),
         sex= as.factor(sex),
         degree= as.factor(degree))


dfUK$res_party2 <- factor(dfUK$res_party2,
                          levels = c(0,1),
                          labels = c("Conservative", "Labour"))
dfUK$IDasked <- factor(dfUK$IDasked,
                       levels = c(0,1),
                       labels = c("Out-group named", "In-group named"))
dfUK$ideo3_self <- factor(dfUK$ideo3_self,
                          levels = c(0,1,2),
                          labels = c("Left", "Moderate", "Right"))
dfUK$extreme <- factor(dfUK$extreme,
                       levels = c(0,1,2),
                       labels = c("Centrist", "Ideologically moderate", "Ideologically radical"))
dfUK$partyIDcat <- factor(dfUK$partyIDcat,
                          levels = c(0,1,2,3,4),
                          labels = c("Conservative", "Labour", "Lib Dem", "Green/SNP/Plaid Cymru", "Reform UK (Brexit party)"))
dfUK$strengthOG <- factor(dfUK$strengthOG,
                          levels = c(1,2,3),
                          labels = c("1. Very strong", "2. Fairly strong", "3. Not very strong"))
dfUK$IDstrength <- factor(dfUK$IDstrength,
                          levels = c(1,2,3,4),
                          labels = c("1. Very strong", "2. Fairly strong", "3. Not very strong", "4. Squeezed"))
dfUK$strengthdummy <- factor(dfUK$strengthdummy,
                             levels = c(0,1),
                             labels = c("Not at all strong", "Very strong"))
dfUK$partynegaffectcat <- factor(dfUK$partynegaffectcat,
                                 levels = c(0,1,2,3),
                                 labels = c("Conservative with low Labour hate", "Conservative with strong Labour hate", "Labour with low Conservative hate", "Labour with strong Conservative hate"))
dfUK$strongdislike <- factor(dfUK$strongdislike,
                             levels = c(0,1),
                             labels = c("Below mean negative out-group affect", "Above mean negative out-group affect"))
dfUK$woman <- factor(dfUK$woman,
                     levels = c(0,1),
                     labels = c("Man", "Woman"))
dfUK$british <- factor(dfUK$british,
                       levels = c(0,1),
                       labels = c("Not British", "British"))
dfUK$queer <- factor(dfUK$queer,
                     levels = c(0,1),
                     labels = c("Cishetero", "LGBT+"))
dfUK$villian <- factor(dfUK$villian,
                       levels = c(0,1),
                       labels = c("Hero", "Villain"))
dfUK$villiancat <- factor(dfUK$villiancat,
                          levels = c(0,1,2,3),
                          labels = c("Strong Hero", "Hero", "Villain", "Strong Villain"))
dfUK$house <- factor(dfUK$house,
                     levels = c(1,2,3,4,5,6),
                     labels = c("Disney", "Game of Thrones", "Harry Potter", "Lord of the Ring", "Marvel", "Star Wars"))
dfUK$imdb_quartiles <- factor(dfUK$imdb_quartiles,
                              levels = c(1,2,3,4),
                              labels = c("Rating Q1", "Rating Q2", "Rating Q3", "Rating Q4"))
dfUK$engagement_quartiles <- factor(dfUK$engagement_quartiles,
                                    levels = c(1,2,3,4),
                                    labels = c("Enagement Q1", "Enagement Q2", "Enagement Q3", "Enagement Q4"))
dfUK$heroes <- factor(dfUK$heroes,
                      levels = c(1,2,3,4,5,6,7,8,9,10,
                                 11,12,13,14,15,16,17,18,19,20,
                                 21,22,23,24,25,26,27,28,29,30,
                                 31,32,33,34,35,36,37,38,39,40,
                                 41,42,43),
                      labels = c("Aladdin", "Albus Dumbledore", "Aragorn", "Arthur Weasley", "Arya Stark", "Bilbo Baggins", "Black Widow", "Brienne of Tarth", "Captain America", "Captain Marvel", "Catelynn Stark", "Cinderella", "Dobby", "Eddard Stark", "Galadriel", "Gandalf the White", "Harry Potter", "Hercules", "Hermione Granger", "Ironman", "Kristoff", "Luke Skywalker", "Mighty Thor","Molly Weasley", "Okoye", "Princess Aurora", "Princess Fiona", "Princess Lea", "Princess Tiana", "Prof. Minerva McGonagall", "Quasimodo", "Rey Palpatine", "Rocket", "Ronald Weasley", "Simba",
                                 "Snow White","Spiderman", "Tarzan", "Thor", "Valkyrie", "Wanda Maximoff", "Yoda", "Éowyn"))

dfUK$noheroes <- factor(dfUK$noheroes,
                        levels = c(1,2,3,4,5,6,7,8,9,10,
                                   11,12,13,14,15,16,17,18,19,20,
                                   21,22,23,24,25,26,27,28,29,30,
                                   31,32,33,34,35,36,37,38,39,40,
                                   41,42,43),
                        labels = c("Agatha Harkness", "Azog", "Bellatrix Lestrange", "Captain Phasma", "Cersei Lannister", "Cruella de Vil", "Darth Maul", "Darth Vader", "Dolores Umbridge", "Dr Facilier", "Draco Malfoy", "Ebony Maw", "Fairy Godmother (Shrek)", "Gaston", "Gorr", "Gríma Wormtongue", "Hades", "Hela", "Joffrey Baratheon", "King Pin", "Kylo Ren", "Lady Tremaine", "Loki", "Lucius Malfoy", "Lurtz", "Maleficent", "Mother Gothel", "Narcissa Malfoy", "Pansy Parkinson", "Petunia Dursley", "Proxima Midnight", "Queen Grimhilde", "Queen of Hearts", "Red Skull (Johann Schmidt)", "Rita Skeeter", "Ronan the Accuser",
                                   "Roose Bolton", "Saruman the Wise", "Scar", "Thanos", "Tywin Lannister", "Ultron", "Ursula"))

labels <- c(villian = "Hero vs Villain", villiancat = "Hero vs. Villain", woman = "Sex",
            house = "Media Franchise", british = "British typecast",
            imdb_quartiles = "IMDB Review", engagement_quartiles= "IDMB engagment")





###FIGURE 2###
colors <- c("#56B4E9", "#C5407EFF")
chardata<- summaryBy(villiannum~ person, data=dfUS)
chardata<- chardata%>% 
  mutate(villiannum= as.factor(villiannum.mean))

chardata$villiannum <- factor(chardata$villiannum,
                              levels = c(1,2),
                              labels = c("Hero", "Villain"))

ProfileMeans_UK<- mm(dfUK, partymatch ~ person, id = ~ IDvar, h0 = 0.5)
ProfileMeans_US<- mm(dfUS, partymatch ~ person, id = ~ IDvar, h0 = 0.5)
ProfileMeans_UK_dist<- mm(dfUK, RILEdummymatch ~ person, id = ~ IDvar, h0 = 0.5)
ProfileMeans_US_dist<- mm(dfUS, RILEdummymatch ~ person, id = ~ IDvar, h0 = 0.5)

ProfileMeans_UK <-ProfileMeans_UK%>% 
  mutate(person= as.factor(level))
ProfileMeans_UK_dist <-ProfileMeans_UK_dist%>% 
  mutate(person= as.factor(level))

ProfileMeans_US <-ProfileMeans_US%>% 
  mutate(person= as.factor(level))
ProfileMeans_US_dist <-ProfileMeans_US_dist%>% 
  mutate(person= as.factor(level))

merged_UK = ProfileMeans_UK %>% 
  left_join(chardata, by = 'person')
merged_UK_dist = ProfileMeans_UK_dist %>% 
  left_join(chardata, by = 'person')

merged_US = ProfileMeans_US %>% 
  left_join(chardata, by = 'person')
merged_US <- subset(merged_US, villiannum!= "NA")
merged_US_dist = ProfileMeans_US_dist %>% 
  left_join(chardata, by = 'person')

merged_US <-merged_US%>% 
  mutate(level= as.factor(villiannum))
herosUS <- subset(merged_US, villiannum.mean==1)
villsUS <- subset(merged_US, villiannum.mean==2)

merged_US_dist <-merged_US_dist%>% 
  mutate(level= as.factor(villiannum))
herosUS_dist <- subset(merged_US_dist, villiannum.mean==1)
villsUS_dist <- subset(merged_US_dist, villiannum.mean==2)

merged_UK <-merged_UK%>% 
  mutate(level= as.factor(villiannum))
herosUK <- subset(merged_UK, villiannum.mean==1)
villsUK <- subset(merged_UK, villiannum.mean==2)

merged_UK_dist <-merged_UK_dist%>% 
  mutate(level= as.factor(villiannum))
herosUK_dist <- subset(merged_UK_dist, villiannum.mean==1)
villsUK_dist <- subset(merged_UK_dist, villiannum.mean==2)


mm.1US  <- mm(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1US<-subset(mm.1US, feature %in% c("villian")) 
mm.1UK  <- mm(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1UK<-subset(mm.1UK, feature %in% c("villian")) 

mm.2US  <- mm(dfUS, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.2US <-subset(mm.2US, feature %in% c("villian")) 
mm.2UK  <- mm(dfUK, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.2UK<-subset(mm.2UK, feature %in% c("villian")) 

MM.1UK$lower95 <- MM.1UK$estimate - (1.96 * MM.1UK$std.error)
MM.1UK$higher95 <- MM.1UK$estimate + (1.96 * MM.1UK$std.error)
MM.1UK$lower99 <- MM.1UK$estimate - (2.58 * MM.1UK$std.error)
MM.1UK$higher99 <- MM.1UK$estimate + (2.58 * MM.1UK$std.error)

MM.2UK$lower95 <- MM.2UK$estimate - (1.96 * MM.2UK$std.error)
MM.2UK$higher95 <- MM.2UK$estimate + (1.96 * MM.2UK$std.error)
MM.2UK$lower99 <- MM.2UK$estimate - (2.58 * MM.2UK$std.error)
MM.2UK$higher99 <- MM.2UK$estimate + (2.58 * MM.2UK$std.error)

MM.1US$lower95 <- MM.1US$estimate - (1.96 * MM.1US$std.error)
MM.1US$higher95 <- MM.1US$estimate + (1.96 * MM.1US$std.error)
MM.1US$lower99 <- MM.1US$estimate - (2.58 * MM.1US$std.error)
MM.1US$higher99 <- MM.1US$estimate + (2.58 * MM.1US$std.error)

MM.2US$lower95 <- MM.2US$estimate - (1.96 * MM.2US$std.error)
MM.2US$higher95 <- MM.2US$estimate + (1.96 * MM.2US$std.error)
MM.2US$lower99 <- MM.2US$estimate - (2.58 * MM.2US$std.error)
MM.2US$higher99 <- MM.2US$estimate + (2.58 * MM.2US$std.error)


main1<- MM.1US %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=3.5,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.35, size = 4)+
  labs(subtitle="Pr(Projected partisanship) - USA", x=NULL, y="") +
  geom_jitter(data = herosUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = villsUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(.3,.7)+
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_text(face="bold"),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0.50, linetype="dashed", colour="gray33")+
  annotate(
    geom="label", x = .6, y =1.9, size = 4, color = "#56b4e9", fontface=2,
    label = "Hero")+
  annotate(geom="label", x = .6, y =2.3, size = 4, color = "darkmagenta", fontface=2,
           label = "Villain")

main2<- MM.1UK %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=3.5,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.35, size = 4)+
  labs(subtitle="Pr(Projected partisanship) - UK", x=NULL, y="") +
  geom_jitter(data = herosUK, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = villsUK, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(.3,.7)+
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.title.y = element_blank(),         
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0.50, linetype="dashed", colour="gray33")


main3<- MM.2US %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=3.5,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.35, size = 4)+
  labs(subtitle="Pr(Projected ideology) - USA", x=NULL, y="") +  
  geom_jitter(data = herosUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = villsUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(.3,.7)+
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_text(face="bold"),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0.50, linetype="dashed", colour="gray33")

main4<- MM.2UK %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=3.5,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.35, size = 4)+
  labs(y=NULL, x=NULL) +
  geom_jitter(data = herosUK_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = villsUK_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  theme_ggdist()+
  theme(legend.position = "none") +
  xlim(.3,.7)+
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="Pr(Projected ideology) - UK", x=NULL, y="") + 
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.title.y = element_blank(),         
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0.50, linetype="dashed", colour="gray33")


study1_all<- (main1+main2) / (main3+main4)+
  plot_annotation(title = 'Study 1: Modelling in-group projection & out-group counter-projection',
                  subtitle = 'Plot reports marginal means',
                  caption = "Confidence intervals at 95% & 99%\nOverlaid observations indicate mean in-group projection of individual characters",
                  theme = theme(plot.title = element_text(face = "bold", size=18),
                                plot.subtitle = element_text(size=14)))
ggsave("figures_main/Figure2.png", width = 24, height = 12, units = "cm")
study1_all


###FIGURE 3###

diffs_US<- mm_diffs(dfUS, partymatch ~ person,h0 = 0.5, ~ strengthdummy, id = ~ IDvar)
diffs_US_dist<- mm_diffs(dfUS, RILEdummymatch ~ person, h0 = 0.5, ~ strengthdummy, id = ~ IDvar)

diffs_UK<- mm_diffs(dfUK, partymatch ~ person,h0 = 0.5, ~ strengthdummy, id = ~ IDvar)
diffs_UK_dist<- mm_diffs(dfUK, RILEdummymatch ~ person, h0 = 0.5, ~ strengthdummy, id = ~ IDvar)

diffs_US <-diffs_US%>% 
  mutate(person= as.factor(level))
diffs_US_dist <-diffs_US_dist%>% 
  mutate(person= as.factor(level))
diffs_UK <-diffs_UK%>% 
  mutate(person= as.factor(level))
diffs_UK_dist <-diffs_UK_dist%>% 
  mutate(person= as.factor(level))

diff_merged_US = diffs_US %>% 
  left_join(chardata, by = 'person')
diff_merged_US <- subset(diff_merged_US, villiannum!= "NA")

diff_merged_US_dist = diffs_US_dist %>% 
  left_join(chardata, by = 'person')
diff_merged_US_dist <- subset(diff_merged_US_dist, villiannum!= "NA")


diff_merged_UK = diffs_UK %>% 
  left_join(chardata, by = 'person')
diff_merged_UK <- subset(diff_merged_UK, villiannum!= "NA")

diff_merged_UK_dist = diffs_UK_dist %>% 
  left_join(chardata, by = 'person')
diff_merged_UK_dist <- subset(diff_merged_UK_dist, villiannum!= "NA")

diff_merged_US <-diff_merged_US%>% 
  mutate(level= as.factor(villiannum))
diff_herosUS <- subset(diff_merged_US, villiannum.mean==1)
diff_villsUS <- subset(diff_merged_US, villiannum.mean==2)

diff_merged_US_dist <-diff_merged_US_dist%>% 
  mutate(level= as.factor(villiannum))
diff_herosUS_dist <- subset(diff_merged_US_dist, villiannum.mean==1)
diff_villsUS_dist <- subset(diff_merged_US_dist, villiannum.mean==2)

diff_merged_UK <-diff_merged_UK%>% 
  mutate(level= as.factor(villiannum))
diff_herosUK <- subset(diff_merged_UK, villiannum.mean==1)
diff_villsUK <- subset(diff_merged_UK, villiannum.mean==2)

diff_merged_UK_dist <-diff_merged_UK_dist%>% 
  mutate(level= as.factor(villiannum))
diff_herosUK_dist <- subset(diff_merged_UK_dist, villiannum.mean==1)
diff_villsUK_dist <- subset(diff_merged_UK_dist, villiannum.mean==2)

mm.diff.party <- mm_diffs(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                          ~ strengthdummy, id = ~ IDvar)
mm.diff.partyX<- subset(mm.diff.party, feature %in% c("villian"))

mm.diff.partyb <- mm_diffs(dfUS, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                           ~ strengthdummy, id = ~ IDvar)
mm.diff.partybX<- subset(mm.diff.partyb, feature %in% c("villian"))

mm.diff.partyX$lower95 <- mm.diff.partyX$estimate - (1.96 * mm.diff.partyX$std.error)
mm.diff.partyX$higher95 <- mm.diff.partyX$estimate + (1.96 * mm.diff.partyX$std.error)
mm.diff.partyX$lower99 <- mm.diff.partyX$estimate - (2.58 * mm.diff.partyX$std.error)
mm.diff.partyX$higher99 <- mm.diff.partyX$estimate + (2.58 * mm.diff.partyX$std.error)

mm.diff.partybX$lower95 <- mm.diff.partybX$estimate - (1.96 * mm.diff.partybX$std.error)
mm.diff.partybX$higher95 <- mm.diff.partybX$estimate + (1.96 * mm.diff.partybX$std.error)
mm.diff.partybX$lower99 <- mm.diff.partybX$estimate - (2.58 * mm.diff.partybX$std.error)
mm.diff.partybX$higher99 <- mm.diff.partybX$estimate + (2.58 * mm.diff.partybX$std.error)

mmUK.diff.party <- mm_diffs(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                            ~ strengthdummy, id = ~ IDvar)
mmUK.diff.partyX<- subset(mmUK.diff.party, feature %in% c("villian"))

mmUK.diff.partyb <- mm_diffs(dfUK, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                             ~ strengthdummy, id = ~ IDvar)
mmUK.diff.partybX<- subset(mmUK.diff.partyb, feature %in% c("villian"))

mmUK.diff.partyX$lower95 <- mmUK.diff.partyX$estimate - (1.96 * mmUK.diff.partyX$std.error)
mmUK.diff.partyX$higher95 <- mmUK.diff.partyX$estimate + (1.96 * mmUK.diff.partyX$std.error)
mmUK.diff.partyX$lower99 <- mmUK.diff.partyX$estimate - (2.58 * mmUK.diff.partyX$std.error)
mmUK.diff.partyX$higher99 <- mmUK.diff.partyX$estimate + (2.58 * mmUK.diff.partyX$std.error)

mmUK.diff.partybX$lower95 <- mmUK.diff.partybX$estimate - (1.96 * mmUK.diff.partybX$std.error)
mmUK.diff.partybX$higher95 <- mmUK.diff.partybX$estimate + (1.96 * mmUK.diff.partybX$std.error)
mmUK.diff.partybX$lower99 <- mmUK.diff.partybX$estimate - (2.58 * mmUK.diff.partybX$std.error)
mmUK.diff.partybX$higher99 <- mmUK.diff.partybX$estimate + (2.58 * mmUK.diff.partybX$std.error)



diff_main1<- mm.diff.partyX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.4, size = 4)+
  labs(y="Strong vs weak\nidentity attachment", x=NULL) +
  geom_jitter(data = diff_herosUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = diff_villsUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.3, .3)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="a) Partisan projection (US)")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")+
  annotate(
    geom="label", x = .2, y =1.4, size = 4, color = "#56b4e9", fontface=2,
    label = "Hero")+
  annotate(geom="label", x = .2, y =1.8, size = 4, color = "darkmagenta", fontface=2,
           label = "Villain")



diff_main2<- mm.diff.partybX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.4, size = 4)+
  labs(y="", x=NULL) +
  geom_jitter(data = diff_herosUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = diff_villsUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.3, .3)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="b) Ideological projection (US)")+  
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")


diff_main3<- mmUK.diff.partyX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.4, size = 4)+
  labs(y="Strong vs weak\nidentity attachment", x=NULL) +
  geom_jitter(data = diff_herosUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = diff_villsUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.3, .3)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="c) Partisan projection (UK)")+  
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")



diff_main4<- mmUK.diff.partybX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.4, size = 4)+
  labs(y="", x=NULL) +
  geom_jitter(data = diff_herosUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = diff_villsUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.3, .3)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="d) Ideological projection (UK)")+  
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")

study1_strength<- (diff_main1+diff_main2) / (diff_main3+diff_main4)+
  plot_annotation(title = 'Study 1: Effects larger among strong identifiers',
                  subtitle = 'Plot reports pairwise difference in the marginal means',
                  caption = 'Overlaid observations indicate mean difference for individual character target',
                  theme = theme(plot.title = element_text(face = "bold", size=18),
                                plot.subtitle = element_text(size=14)))

ggsave("figures_main/Figure3.png", width = 24, height = 12, units = "cm")
study1_strength

###FIGURE 7###

partydiff<- mm_diffs(dfUS, partymatch ~ person,h0 = 0.5, ~ res_party2, id = ~ IDvar)
partydiff_dist<- mm_diffs(dfUS, RILEdummymatch ~ person, h0 = 0.5, ~ res_party2, id = ~ IDvar)
partydiff <-partydiff%>% 
  mutate(person= as.factor(level))
partydiff_dist <-partydiff_dist%>% 
  mutate(person= as.factor(level))

party_merged = partydiff %>% 
  left_join(chardata, by = 'person')
party_merged <- subset(party_merged, villiannum!= "NA")

party_merged_dist = partydiff_dist %>% 
  left_join(chardata, by = 'person')
party_merged_dist <- subset(party_merged_dist, villiannum!= "NA")

party_merged <-party_merged%>% 
  mutate(level= as.factor(villiannum))
party_herosUS <- subset(party_merged, villiannum.mean==1)
party_villsUS <- subset(party_merged, villiannum.mean==2)

party_merged_dist <-party_merged_dist%>% 
  mutate(level= as.factor(villiannum))
party_herosUS_dist <- subset(party_merged_dist, villiannum.mean==1)
party_villsUS_dist <- subset(party_merged_dist, villiannum.mean==2)


mm.diff.party <- mm_diffs(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                          ~ res_party2, id = ~ IDvar)
mm.diff.partyX<- subset(mm.diff.party, feature %in% c("villian"))

mm.diff.partyb <- mm_diffs(dfUS, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                           ~ res_party2, id = ~ IDvar)
mm.diff.partybX<- subset(mm.diff.partyb, feature %in% c("villian"))

mm.diff.partyX$lower95 <- mm.diff.partyX$estimate - (1.96 * mm.diff.partyX$std.error)
mm.diff.partyX$higher95 <- mm.diff.partyX$estimate + (1.96 * mm.diff.partyX$std.error)
mm.diff.partyX$lower99 <- mm.diff.partyX$estimate - (2.58 * mm.diff.partyX$std.error)
mm.diff.partyX$higher99 <- mm.diff.partyX$estimate + (2.58 * mm.diff.partyX$std.error)

mm.diff.partybX$lower95 <- mm.diff.partybX$estimate - (1.96 * mm.diff.partybX$std.error)
mm.diff.partybX$higher95 <- mm.diff.partybX$estimate + (1.96 * mm.diff.partybX$std.error)
mm.diff.partybX$lower99 <- mm.diff.partybX$estimate - (2.58 * mm.diff.partybX$std.error)
mm.diff.partybX$higher99 <- mm.diff.partybX$estimate + (2.58 * mm.diff.partybX$std.error)

party_main1<- mm.diff.partyX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.2, size = 4)+
  labs(y="Pairwise difference in MM\nDemocratic - Republican", x=NULL) +
  geom_jitter(data = party_herosUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = party_villsUS, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.5, .4)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="a) Left more likely to project than the right (partisan identities)")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")+
  annotate(
    geom="label", x = .3, y =1.5, size = 4, color = "#56b4e9", fontface=2,
    label = "Hero")+
  annotate(geom="label", x = .3, y =1.7, size = 4, color = "darkmagenta", fontface=2,
           label = "Villain")

party_main2<- mm.diff.partybX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.2, size = 4)+
  labs(y="", x=NULL) +
  geom_jitter(data = party_herosUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = party_villsUS_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.5, .4)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="b) Counter-projection larger than projection (ideological identities)")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")


collapsed.dfUS<- summaryBy(outgrouplike + outgroupdislike + knowtotal + res_party2~ IDvar, data=dfUS)
collapsed.dfUS <- collapsed.dfUS %>% 
  mutate(res_party2 = as.factor(res_party2.mean),
         knowtotal = as.numeric(knowtotal.mean))

collapsed.dfUS <- subset(collapsed.dfUS, !is.na(res_party2))
collapsed.dfUS <- subset(collapsed.dfUS, !is.na(knowtotal))

model_outUS <-lm (outgroupdislike.mean ~ res_party2, collapsed.dfUS)
summary(model_outUS)
collapsed.dfUS$predicted<-predict(model_outUS, collapsed.dfUS)
knowmeanUS <- ddply(collapsed.dfUS, "res_party2", summarise, meanx2=mean(knowtotal))

collapsed.dfUS <- collapsed.dfUS %>% 
  mutate(outgroupdislike.mean = outgroupdislike.mean/10)
partymeanUS <- ddply(collapsed.dfUS, "res_party2", summarise, meanx=mean(outgroupdislike.mean))


UShist<- ggplot(collapsed.dfUS, aes(x=outgroupdislike.mean, fill=res_party2)) +
  geom_density(alpha=.8) +
  theme_ggdist()+
  xlim(0,1)+
  scale_fill_manual(values = c("red2", "blue2"), labels=c("Republicans", "Democrats")) +
  scale_color_manual(values = c("red2", "blue2")) +
  geom_vline(data=partymeanUS, aes(xintercept=meanx,  colour=res_party2),
             linetype="dashed", size=1, alpha=.5)+
  labs(y="Density", x="(Self-reported) negative out-group affect", subtitle="c) Left hold more negative affects towards out-group than vice versa ")+
  theme(legend.position = "none",
        plot.subtitle = element_text(face="bold"))+
  annotate("label", x = 0, y = 2.15, label = "Republicans",
           color="red2", size=4, fontface="bold", hjust=0)+
  annotate("label", x = 0, y = 2.35, label = "Democrats",
           color="blue2", size=4, fontface="bold", hjust=0)

modelparty<-lm (partymatch ~ villian*outgroupdislike, data=dfUS)
dfUS$predictINT1<-predict(modelparty, dfUS)

modelID<-lm (RILEdummymatch ~ villian*outgroupdislike, data=dfUS)
dfUS$predictINT2<-predict(modelID, dfUS)

treat <- subset(dfUS, villian== "Hero")
control <- subset(dfUS, villian== "Villain")

gg_df1 <-
  modelparty %>%
  margins(at = list(outgroupdislike = seq(0, 10, by = 1))) %>%
  summary %>%
  as.data.frame() %>%
  filter(factor == "villianVillain")

gg_df2 <-
  modelID %>%
  margins(at = list(outgroupdislike = seq(0, 10, by = 1))) %>%
  summary %>%
  as.data.frame() %>%
  filter(factor == "villianVillain")

predplot1 <-
  ggplot(dfUS, aes(outgroupdislike, partymatch, group = villian, color=villian)) +
  scale_color_manual(values = c("#56B4E9", "#C5407EFF")) +
  scale_fill_manual(values = c("#56B4E9", "#C5407EFF")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  theme(legend.position = "none")+
  xlim(0, 10)+
  ylab("Pr(Projected partisanship)")+
  xlab("(Self-reported) negative out-group affect")+
  labs(subtitle="d) Negative out-group affect determines Pr(Shared partisanship)")+
  theme(axis.text.x =element_blank(),
        axis.ticks.x =element_blank(),
        plot.subtitle = element_text(face="bold"))+
  annotate(
    geom="label", x = 7.5, y =.525, size = 5, color="#56B4E9", fontface=2,
    label = "Slope for heroes")+
  annotate(
    geom="label", x = 7.5, y =.475, size = 5, color="#C5407EFF", fontface=2,
    label = "Slope for villains")


predplot2 <-
  ggplot(dfUS, aes(outgroupdislike, RILEdummymatch, group = villian, color=villian)) +
  scale_color_manual(values = c("#56B4E9", "#C5407EFF")) +
  scale_fill_manual(values = c("#56B4E9", "#C5407EFF")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  theme(legend.position = "none",
        plot.subtitle = element_text(face="bold"))+
  xlim(0, 10)+
  ylab("Pr(Projeced ideology)")+
  xlab("(Self-reported) negative out-group affect")+
  labs(subtitle="e) Negative out-group affect determines Pr(Shared ideology)")


study1_party<- (party_main1+party_main2) / UShist /(predplot1+predplot2)
study1_party+
  plot_annotation(title = 'Study 1: Party & affect-based exploratory analyses',
                  caption = 'British data from Study 1 reported in supplementary material',
                  theme = theme(plot.title = element_text(face = "bold", size=18)))
ggsave("figures_main/Figure7.png", width = 32, height = 32, units = "cm")


